package com.heima.homework;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo01 {

    public static void main(String[] args) throws Exception {
        // 1.从文件读取数据
        FileReader fr = new FileReader("D:\\JAVA\\JavaSE进阶-相关资料\\课后作业\\day10\\hero.txt", Charset.forName("GBK"));
        BufferedReader br = new BufferedReader(fr);
        String head = br.readLine();// 表头
        String content;


        // 创建集合用于存储 英雄对象
        ArrayList<Hero> heroes = new ArrayList<>();
        while ((content = br.readLine()) != null) {
            // 2.读取的数据封装成对象
            String[] split = content.split(",+");
            Hero hero = new Hero(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]));
            System.out.println(split[3]);
            heroes.add(hero);
        }

        br.close();
        // 3.对对象进行排序
        Collections.sort(heroes, new Comparator<Hero>() {
            @Override
            public int compare(Hero o1, Hero o2) {
                return Double.compare(o2.getScore(), o1.getScore());
            }
        });
        System.out.println(heroes);

        /*// 4.写回到文件
        FileWriter fw = new FileWriter("hero.txt", Charset.forName("GBK"));
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(head+" 新手适应度分值");
        bw.newLine();

        for (Hero hero : heroes) {
            bw.write(hero.getName()+","+hero.getHealth()+","+hero.getAttack()+","+hero.getEffect()+","+hero.getScore());
            bw.newLine();
        }*/
        /*bw.flush();
        bw.close();*/
    }
}
